package com.sat.order.controller;

import com.sat.order.pojo.Cart;
import com.sat.order.pojo.OrderItem;
import com.sat.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    /***
     * 加入购物车
     * @param num:购买的数量
     * @param id：购买的商品(SKU)ID
     * @return
     */
    @RequestMapping(value = "/add")
    public Result add(Integer num, Long id){
        //用户名
        String username="hzit";
        //将商品加入购物车
        cartService.add(num,id,username);
        return new Result(true, StatusCode.OK,"加入购物车成功！");
    }

    /***
     * 查询用户购物车列表
     * @return
     */
    @GetMapping(value = "/list")
    public Result<List<OrderItem>> list(){
        //用户名
        String username="hzit";
        List<OrderItem> list = cartService.list(username);
        return new Result<List<OrderItem>>(true,StatusCode.OK,"购物车列表查询成功！",list);
    }

    @RequestMapping(value = "/update")
    public Result update(Integer num,Long id){
        //用户名
        String username="hzit";
        cartService.update(num,id,username);
        return new Result(true,StatusCode.OK,"修改成功！");
    }

    @RequestMapping(value = "/delete")
    public Result delete(Long id){
        //用户名
        String username="hzit";
        cartService.delete(id,username);
        return new Result(true,StatusCode.OK,"删除成功！");
    }

    @RequestMapping(value = "/deleteAll")
    public Result deleteAll(){
        //用户名
        String username="hzit";
        cartService.deleteAll(username);
        return new Result(true,StatusCode.OK,"删除全部成功！");
    }



}
